﻿using uTrade.Core;

namespace uTrade.Strategies
{
    internal class MACD_Cross : Strategy
    {
        private DataSeries diff;

        public override void Initialize()
        {
            diff = MACD(this.Close, 12, 26, 9).Diff;
        }

        public override void OnBarUpdate()
        {
            if (diff[1] > 0 && diff[2] <= 0) //cross up
            {
                if (this.PositionShort > 0)
                    BuyToCover(this.PositionShort, Open[0]);
                Buy(1, Open[0]);
            }
            else if (diff[1] < 0 && diff[2] >= 0) //cross dn
            {
                if (this.PositionLong > 0)
                    Sell(PositionLong, Open[0]);
                SellShort(1, Open[0]);
            }
        }
    }
}